﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.IO;
using System.Data;
using CE.InputOut;
/// <summary>
/// Summary description for MyLocation
/// </summary>
/// 
namespace CE.StreetDirectory
{

    public class MyLocation
    {
        const string API_KEY = "5f127af2c23ec2c7b9a495ff2cb70dfd1f74098f";
        public MyLocation()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public string GetAddressByPostCode(string strPostCode)
        {
            string anstr = "";
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://www.streetdirectory.com/api/search/sg_adv.php? " + @"&api=" + API_KEY + "&pc=" + strPostCode);
                req.Method = "GET";
                req.Referer = "http://www.comparexpress.com";
                HttpWebResponse webResponse = (HttpWebResponse)req.GetResponse();
                Stream stream = webResponse.GetResponseStream();
                StreamReader answer = new StreamReader(stream);
                anstr = answer.ReadToEnd();
            }
            catch (WebException Wex)
            {
                CreateExceptionLog(Wex, "string GetAddressByPostCode(string strPostCode)");
            }
            catch (Exception Ex)
            {
                CreateExceptionLog(Ex, "string GetAddressByPostCode(string strPostCode)");
            }
            return anstr;
        }

        public DataSet GetAddressByPostCodeDataSet(string strPostCode)
        {
            DataSet _ds = null;
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://www.streetdirectory.com/api/search/sg_adv.php? " + @"&api=" + API_KEY + "&type=xml&pc=" + strPostCode);
                req.Method = "GET";
                req.Referer = "http://www.comparexpress.com";
                HttpWebResponse webResponse = (HttpWebResponse)req.GetResponse();
                Stream stream = webResponse.GetResponseStream();
                StreamReader answer = new StreamReader(stream);
                _ds = new DataSet("DsAddressList");
                _ds.ReadXml(stream);
            }
            catch (WebException Wex)
            {
                CreateExceptionLog(Wex, "DataSet GetAddressByPostCodeDataSet(string strPostCode)");
            }
            catch (Exception Ex)
            {
                CreateExceptionLog(Ex, "DataSet GetAddressByPostCodeDataSet(string strPostCode)");
            }
            //String anstr = answer.ReadToEnd();
            return _ds;
        }

        private void CreateExceptionLog(Exception Ex, string strLocation)
        {
            String[] _fileContent = new String[6];
            _fileContent[0] = "Source : " + Ex.Source;
            _fileContent[1] = "Date Time : " + CE.Global.General.GetUTCDateTime().ToString();
            _fileContent[2] = "Stack Trace :" + Ex.StackTrace.ToString();
            _fileContent[3] = "Target Site : " + Ex.TargetSite.ToString();
            _fileContent[4] = "Location : " + "CE.Database.MyDatabase - " + strLocation;
            _fileContent[5] = "Message : " + Ex.Message;

            WriteExceptionLog("StreetDirectory_", _fileContent);
        }

        private void WriteExceptionLog(string strPrefixFileName, string[] strException)
        {
            try
            {
                MyIO _newFile = new MyIO();
                string _filePath = AppDomain.CurrentDomain.BaseDirectory + "Exception\\";
                if (_newFile.CheckDirectory(_filePath))
                {
                    string _curMonth = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00");

                    if (!_newFile.CheckDirectory(_filePath + _curMonth))
                    {
                        if (_newFile.CreateDirectory(_filePath, _curMonth))
                        {
                            _filePath += _curMonth + "\\";
                        }
                    }
                    else
                    {
                        _filePath += _curMonth + "\\";
                    }

                    string _fileName = strPrefixFileName + _curMonth + CE.Global.General.GetUTCDateTime().Day.ToString("00") + CE.Global.General.GetUTCDateTime().Second.ToString("00") + CE.Global.General.GetUTCDateTime().Millisecond.ToString("00") + ".txt";

                    _newFile.WriteNewFile(_filePath, _fileName, strException);
                    _newFile = null;
                }
            }
            catch (Exception Ex)
            {
                Ex = null;
            }
        }
    }
}
